//
//  XNFixPurchaseViewController.m
//  XINNingOA
//
//  Created by samuel on 2022/3/29.
//

#import "XNFixPurchaseViewController.h"
#import "XNFixPurchaseBillInfoRequest.h"
#import "XNFixPurchaseApproveRequest.h"

@interface XNFixPurchaseViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNFixPurchaseBillInfoModel *infoModel;

@end

@implementation XNFixPurchaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNFixPurchaseBillInfoRequest *infoRequest = [[XNFixPurchaseBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNFixPurchaseBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNFixPurchaseBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNFixPurchaseApproveRequest *approveRequest = [[XNFixPurchaseApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNFixPurchaseApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNFixPurchaseApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}


- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              [NSString stringWithFormat:@"申请部门： %@",self.infoModel.departmentName],
                              [NSString stringWithFormat:@"申请人： %@",self.infoModel.applicantName],
                              [NSString stringWithFormat:@"申请说明： %@",self.infoModel.reason.length > 0 ? self.infoModel.reason : @""],
                              [NSString stringWithFormat:@"供应商： %@",self.infoModel.supplierName],
                              [NSString stringWithFormat:@"采购人： %@",self.infoModel.confirmUserName],
                              [NSString stringWithFormat:@"总金额： %@",self.infoModel.totalCostStr],
                              [NSString stringWithFormat:@"是否暂支： %@",self.infoModel.isTemp ? @"是":@"否"],
                              [NSString stringWithFormat:@"询价记录： %@",self.infoModel.enquiryInfo],
                              [NSString stringWithFormat:@"采购说明： %@",self.infoModel.remark.length > 0 ? self.infoModel.remark:@""],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    if (self.infoModel.isTemp) {
        XNTableShowDataModel *advanceModel = [[XNTableShowDataModel alloc] init];
        advanceModel.showType = XNShowType_Normal;
        advanceModel.title = @"暂支单";
        XNShowItemModel *advanceItemModel = [[XNShowItemModel alloc] init];
        NSArray *advanceShowItemArray = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"暂支事由： %@",self.infoModel.tempReason],
                                   [NSString stringWithFormat:@"暂支金额： %.3f",self.infoModel.tempCost],
                                   [NSString stringWithFormat:@"预计归还日期： %@",self.infoModel.returnDateStr],
                                   nil];
        advanceItemModel.showItemArray = advanceShowItemArray;
        [advanceModel.showDataArray addObject:advanceItemModel];
        [self.showArray addObject:advanceModel];
    }
    
    XNTableShowDataModel *showModel3 = [[XNTableShowDataModel alloc] init];
    showModel3.showType = XNShowType_Normal;
    showModel3.title = @"子表信息";
    [self.infoModel.itemList enumerateObjectsUsingBlock:^(XNSubFixPurchaseItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
        NSArray *showItemArray3 = [NSArray arrayWithObjects:
                                       [NSString stringWithFormat:@"固资类别: %@",obj.fixedTypeName],
                                       [NSString stringWithFormat:@"固资名称： %@",obj.assetName],
                                       [NSString stringWithFormat:@"规格型号： %@",obj.modelNumber],
                                   [NSString stringWithFormat:@"尺寸： %@",obj.size.length > 0 ? obj.size : @""],
                                   [NSString stringWithFormat:@"数量： %.3f",obj.num],
                                   [NSString stringWithFormat:@"单价： %.3f",obj.price],
                                   [NSString stringWithFormat:@"总价： %.3f",obj.totalPrice],
                                   [NSString stringWithFormat:@"备注: %@",obj.remark.length > 0 ? obj.remark : @""],
                                       nil];
        itemModel3.showItemArray = showItemArray3;
        [showModel3.showDataArray addObject:itemModel3];
    }];
    [self.showArray addObject:showModel3];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
}


@end
